package com.example.rabbitdemo;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {

    @BindView(R.id.btn_RabbitMqR)
    Button btnRabbitMqR;
    @BindView(R.id.btn_RabbitMqS)
    Button btnRabbitMqS;
    @BindView(R.id.et_ip)
    EditText etIp;
    @BindView(R.id.et_port)
    EditText etPort;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_pas)
    EditText etPas;
    @BindView(R.id.et_exchange)
    EditText etExchange;
    @BindView(R.id.et_rkey)
    EditText etRkey;
    @BindView(R.id.button)
    Button button;
    boolean MQstats = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initview();



    }

    private void initview() {
        etIp.setText(Constants.IpHost);
        etPort.setText(String.valueOf(Constants.Port));
        etName.setText(Constants.username);
        etPas.setText(Constants.password);
        etExchange.setText(Constants.MQ_EXCHANGE_CAR);
        etRkey.setText(Constants.routingKey);
    }


    @OnClick({R.id.btn_RabbitMqR, R.id.btn_RabbitMqS})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_RabbitMqR:
                BlockingDeque<String> queue = new LinkedBlockingDeque<>();
                RabbitMqUtils.publishToAMPQ(queue);
                RabbitMqUtils.publishMessage(queue);

                Toast.makeText(this, "发送成功", Toast.LENGTH_SHORT).show();

                break;
            case R.id.btn_RabbitMqS:
                Intent intent =new Intent(this,RmService.class);

                if (MQstats) {
                    stopService(intent);
                    Toast.makeText(this, "停止监听", Toast.LENGTH_SHORT).show();
                    MQstats=false;
                } else {
                    startService(intent);
                    Toast.makeText(this, "监听成功", Toast.LENGTH_SHORT).show();
                    MQstats=true;


                }

                break;
        }
    }



    @OnClick(R.id.button)
    public void onClick() {
        Constants.IpHost = etIp.getText().toString();
        Constants.Port = Integer.parseInt(etPort.getText().toString());
        Constants.username = etName.getText().toString();
        Constants.password = etPas.getText().toString();
        Constants.MQ_EXCHANGE_CAR = etExchange.getText().toString();
        Constants.routingKey = etRkey.getText().toString();
        Toast.makeText(this, "设置成功", Toast.LENGTH_SHORT).show();
    }
}
